package com.lingshi.yingzaitownmap.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingshi.yingzaitownmap.entity.domain.*;
import com.lingshi.yingzaitownmap.entity.result.Result;
import com.lingshi.yingzaitownmap.enums.SpotTypeEnum;
import com.lingshi.yingzaitownmap.service.*;
import com.lingshi.yingzaitownmap.mapper.RouteMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 刘瑞鹏
* @description 针对表【route(路线表)】的数据库操作Service实现
* @createDate 2025-07-06 17:44:38
*/
@Service
@RequiredArgsConstructor
public class RouteServiceImpl extends ServiceImpl<RouteMapper, Route> implements RouteService{

    private final RouteDetailService routeDetailService;
    private final AttractionService attractionService;
    private final ShopsService shopsService;
    private final RestaurantService restaurantService;

    /**
     * 根据ID获取路线详细信息
     */
    @Override
    public Result getRouteDetailsById(String id) {
        // 根据id获取路线
        Route info = getById(id);
        // 非空判断
        if(info == null){
            throw new RuntimeException("路线 id错误");
        }
        /*
        查询路线经过的地点
         */
        // 查询关联了路线的地点,根据权重升序排序
        List<RouteDetail> spots = routeDetailService.query()
                .eq("route_id", id).orderByAsc("sorted_order").list();
        // 根据类型获取地点信息
        List<Object> list = new ArrayList<>();
        if(spots != null && spots.size() > 0){
            list = spots.stream()
                    .map(spot -> {
                        HashMap<String, Object> map = new HashMap<>(); //封装
                        if (spot.getSpotType().equals(SpotTypeEnum.ATTRACTION.getCode())) { // 景点
                            // 查询
                            Attraction attraction = attractionService.query().eq("id", spot.getSpotId()).one();
                            if (attraction != null) {
                                map.put("id", attraction.getId());
                                map.put("name", attraction.getName());
                            }
                            return map;
                        } else if (spot.getSpotType().equals(SpotTypeEnum.SHOP.getCode())) { // 商铺
                            // 查询
                            Shops shop = shopsService.query().eq("id", spot.getSpotId()).one();
                            if (shop != null) {
                                map.put("id", shop.getId());
                                map.put("name", shop.getName());
                            }
                            return map;
                        } else if (spot.getSpotType().equals(SpotTypeEnum.RESTAURANT.getCode())) { // 餐厅
                            // 查询
                            Restaurant restaurant = restaurantService.query().eq("id", spot.getSpotId()).one();
                            if (restaurant != null) {
                                map.put("id", restaurant.getId());
                                map.put("name", restaurant.getName());
                            }
                            return map;
                        }
                        return null;
                    }).collect(Collectors.toList());
        }
        // 封装
        HashMap<String, Object> result = new HashMap<>();
        result.put("id", info.getId());
        result.put("name", info.getName());
        result.put("stops",list);
        // 返回
        return Result.success(result);
    }
}




